package org.melanesia.beans;

import java.util.Collection;
import java.util.Map;

/**
 * Property setter using collection getter.
 *
 * @author marcin.kielar
 */
public final class CollectionGetSetter implements Setter {

    /** Collection getter. */
    private final Getter getter;

    /**
     * Constructs new {@code CollectionGetSetter} for accessing the property
     * using collection getter.
     *
     * @param getter
     *            getter for accessing collection property
     */
    public CollectionGetSetter(final Getter getter) {
        this.getter = getter;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public void set(final Object bean, final Object value, final Map<String, Class<?>> intermediateBeanClasses) {
        ((Collection) getter.get(bean)).addAll((Collection) value);
    }

    @Override
    public Class<?> getAcceptedType() {
        return getter.getReturnType();
    }

}
